package com.heba.popupwindow;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.heba.bean.ItemShareObject;
import com.heba.bean.ShareObject;
import com.heba.config.CommonConfig;
import com.heba.greenmulu.R;
import com.heba.popupwindow.callback.ShareListener;
import com.heba.utils.DensityUtils;

import java.util.List;


/**
 * 分享彈出框
 * Created by shenghao on 2020/2/07.
 */
public class SharePopupWindow extends BasePopupWindow implements View.OnClickListener {

    private TextView tv_share_title;
    private LinearLayout ll_share_view1;
    private LinearLayout ll_share_view2;
    private TextView tv_line;
    private Button btn_cancel;

    private ShareListener listener;

    public SharePopupWindow(Context context) {
        super(LayoutInflater.from(context).inflate(
                R.layout.popup_share, null));
        setAnimationStyle(R.style.Popup_Animation_Translate);
    }

    public SharePopupWindow(Context context, int width, int height) {
        super(context, LayoutInflater.from(context).inflate(
                R.layout.popup_share, null), width, height);
        setAnimationStyle(R.style.Popup_Animation_Translate);
    }

    @Override
    public void initViews() {
        tv_share_title = (TextView) findViewById(R.id.tv_share_title);
        tv_line = (TextView) findViewById(R.id.tv_line);
        ll_share_view1 = (LinearLayout) findViewById(R.id.ll_share_view1);
        ll_share_view2 = (LinearLayout) findViewById(R.id.ll_share_view2);
        btn_cancel = (Button) findViewById(R.id.btn_cancel);
    }

    @Override
    public void initEvents() {
        btn_cancel.setOnClickListener(this);
    }

    @Override
    public void init() {

    }

    /**
     * 設置分享數據
     *
     * @param shareList1
     * @param shareList2
     */
    public void setShareList(List<ItemShareObject> shareList1, List<ItemShareObject> shareList2) {
        updateShareView(ll_share_view1, shareList1);
        updateShareView(ll_share_view2, shareList2);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
//                if (listener != null)
//                    listener.onCancelClick();
                break;
            default:
                break;
        }
        dismiss();
    }

    /**
     * 添加分享view(分享至facebook、whatsapp、微信、朋友圈)
     * 收藏產品 舉報 編輯等
     */
    private void updateShareView(LinearLayout linearView, List<ItemShareObject> list) {
        linearView.removeAllViews();

        /**
         * 添加佈局參數
         */
        int width = (DensityUtils.getWidth(context) - DensityUtils.dip2px(context, 30)) / 4;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);

        for (int i = 0; i < list.size(); i++) {
            View view = updateShareItemView(list.get(i));
            linearView.addView(view, params);
        }
    }

    /**
     * 設置分享title
     *
     * @param shareTitle
     */
    public void setShareTitle(String shareTitle) {
        tv_share_title.setText(shareTitle);
    }

    /**
     * 设置需要分享的实体类
     *
     * @param shareObject
     */
    public void setShareObject(ShareObject shareObject) {
        if (null != listener) {
            listener.setShareObject(shareObject);
        }
    }

    /**
     * 隱藏line
     */
    public void hideShareLine() {
        tv_line.setVisibility(View.GONE);
    }

    /**
     * share item view
     */
    private View updateShareItemView(ItemShareObject object) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_share_view, null);
        ImageView iv_share_image = view.findViewById(R.id.iv_share_image);
        TextView tv_share_content = view.findViewById(R.id.tv_share_content);
        iv_share_image.setImageResource(object.getImageID());
        tv_share_content.setText(object.getShareContent());

        view.setOnClickListener(v -> {

            if (null == listener)
                return;

            switch (object.getShareType()) {
                case CommonConfig.SHARE_TO_FACEBOOK:
                    listener.shareFacebook();
                    break;
                case CommonConfig.SHARE_TO_WHATSAPP:
                    listener.shareWhatsApp();
                    break;
                case CommonConfig.SHARE_TO_WECHAT:
                    listener.shareWechat();
                    break;
                case CommonConfig.SHARE_TO_CIRCLE:
                    listener.shareCircle();
                    break;
                case CommonConfig.SHARE_TO_SEND_PRODUCT:
                    listener.sendProduct();
                    break;
                case CommonConfig.SHARE_TO_COLLECT_PRODUCT:
                    listener.collectProduct();
                    break;
                case CommonConfig.SHARE_TO_UPDATE_PRODUCT:
                    listener.updateProduct();
                    break;
                case CommonConfig.SHARE_TO_COLLECT_DIARY:
                    listener.collectDiary();
                    break;
                case CommonConfig.SHARE_TO_REPORT_DIARY:
                    listener.reportDiary();
                    break;
                case CommonConfig.SHARE_TO_SEND_USER:
                    listener.sendUser();
                    break;
                case CommonConfig.SHARE_TO_REPORT_USER:
                    listener.reportUser();
                    break;
                case CommonConfig.SHARE_TO_SMS:
                    listener.shareSMS();
                    break;
                case CommonConfig.SHARE_TO_DELETE_DIARY:
                    listener.deleteDiary();
                    break;
                case CommonConfig.SHARE_TO_TUTORIAL:
                    listener.goTutorial();
                    break;
                case CommonConfig.SHARE_TO_HIDDEN_DIARY:
                    listener.hiddenDiary();
                    break;
                case CommonConfig.SHARE_TO_REMOVE_TAG:
                    listener.removeTag();
                    break;
                    case CommonConfig.BLOCK_USER:
                        listener.blockUser();
                        break;
                default:
                    break;
            }
            dismiss();
        });

        return view;
    }


    /**
     * @param listener
     */
    public void setOnShareListener(ShareListener listener) {
        this.listener = listener;
    }

}
